﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Navigation;
using RC = ActiproSoftware.Windows.Controls.Ribbon.Controls;
using System.Windows.Threading;
namespace AppDemo
{
    public interface IMsgBoxWindow
    {
        object MsgBoxContent { get; set; }
        string Caption { get; set; }
        string OkContent { get; set; }
        string YesContent { get; set; }
        string NoContent { get; set; }
        string CancelContent { get; set; }
        MessageBoxImage MsgBoxImage { get; set; }
        MessageBoxButton MsgBoxButton { get; set; }
        MessageBoxResult Show();
    }

    /// <summary>
    /// Interaction logic for MessageWindowDialog.xaml
    /// </summary>
    public partial class MessageWindowDialog : Window, IMsgBoxWindow
    {
        public MessageWindowDialog()
        {
            InitializeComponent();
            
        }

        public MessageBoxResult Show()
        {
            ShowMsgBoxButton();
            AutomationClose();
            base.ShowDialog();
            return this.MsgBoxResult;
        }

        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                base.DragMove();
            }
        }

        private void AutomationClose()
        {
            if (AutoClose)
            {
                int gap = AutoCloseSec;
                string tip = AutoCloseTip;
                AutoCloseTip = string.Format(tip, gap);
                DispatcherTimer timer = new DispatcherTimer();
                timer.Interval = TimeSpan.FromSeconds(1);
                timer.Tick += delegate
                {
                    if (this.IsLoaded)
                    {
                        gap--;
                        AutoCloseTip = string.Format(tip, gap);
                        if (gap == 0)
                        {
                            this.Close();
                            timer.Stop();
                            timer = null;
                        }
                    }
                };
                timer.Start();
            }
        }

        public bool AutoClose
        {
            get { return (bool)GetValue(AutoCloseProperty); }
            set { SetValue(AutoCloseProperty, value); }
        }

        public static readonly DependencyProperty AutoCloseProperty =
            DependencyProperty.Register("AutoClose", typeof(bool), typeof(MessageWindowDialog),
            new UIPropertyMetadata(false));

        public int AutoCloseSec
        {
            get { return (int)GetValue(AutoCloseSecProperty); }
            set { SetValue(AutoCloseSecProperty, value); }
        }

        public static readonly DependencyProperty AutoCloseSecProperty =
            DependencyProperty.Register("AutoCloseSec", typeof(int), typeof(MessageWindowDialog), 
            new UIPropertyMetadata(5));

        public string AutoCloseTip
        {
            get { return (string)GetValue(AutoCloseTipProperty); }
            set { SetValue(AutoCloseTipProperty, value); }
        }

        public static readonly DependencyProperty AutoCloseTipProperty =
            DependencyProperty.Register("AutoCloseTip", typeof(string), typeof(MessageWindowDialog),
            new UIPropertyMetadata("窗口将在{0}秒内自动关闭"));

        public bool IsShowDetail
        {
            get { return (bool)GetValue(ShowDetailProperty); }
            set { SetValue(ShowDetailProperty, value); }
        }

        public static readonly DependencyProperty ShowDetailProperty =
            DependencyProperty.Register("ShowDetail", typeof(bool), typeof(MessageWindowDialog), 
            new UIPropertyMetadata(false));

        public object MsgBoxDetail
        {
            get { return (object)GetValue(MsgBoxDetailProperty); }
            set { SetValue(MsgBoxDetailProperty, value); }
        }

        public static readonly DependencyProperty MsgBoxDetailProperty =
            DependencyProperty.Register("MsgBoxDetail", typeof(object), typeof(MessageWindowDialog),
            new UIPropertyMetadata((o, a) => {
                var element = o as MessageWindowDialog;
                element.IsShowDetail = true;
            }));

        public object MsgBoxContent
        {
            get { return (object)GetValue(MsgBoxContentProperty); }
            set { SetValue(MsgBoxContentProperty, value); }
        }

        public static readonly DependencyProperty MsgBoxContentProperty =
            DependencyProperty.Register("MsgBoxContent", typeof(object), typeof(MessageWindowDialog), 
            new UIPropertyMetadata());

        public string Caption
        {
            get { return (string)GetValue(CaptionProperty); }
            set { SetValue(CaptionProperty, value); }
        }

        public static readonly DependencyProperty CaptionProperty =
            DependencyProperty.Register("Caption", typeof(string), typeof(MessageWindowDialog),
            new UIPropertyMetadata("WMS 8200"));

        public string OkContent
        {
            get { return (string)GetValue(OkContentProperty); }
            set { SetValue(OkContentProperty, value); }
        }

    
        public static readonly DependencyProperty OkContentProperty =
            DependencyProperty.Register("OkContent", typeof(string), typeof(MessageWindowDialog), 
            new UIPropertyMetadata("OK"));

        public string YesContent
        {
            get { return (string)GetValue(YesContentProperty); }
            set { SetValue(YesContentProperty, value); }
        }

        public static readonly DependencyProperty YesContentProperty =
            DependencyProperty.Register("YesContent", typeof(string), typeof(MessageWindowDialog), 
            new UIPropertyMetadata("是(Y)"));

        public string NoContent
        {
            get { return (string)GetValue(NoContentProperty); }
            set { SetValue(NoContentProperty, value); }
        }

        public static readonly DependencyProperty NoContentProperty =
            DependencyProperty.Register("NoContent", typeof(string), typeof(MessageWindowDialog), 
            new UIPropertyMetadata("否(N)"));

        public string CancelContent
        {
            get { return (string)GetValue(CancelContentProperty); }
            set { SetValue(CancelContentProperty, value); }
        }

        public static readonly DependencyProperty CancelContentProperty =
            DependencyProperty.Register("CancelContent", typeof(string), typeof(MessageWindowDialog), 
            new UIPropertyMetadata("取消"));

        public MessageBoxImage MsgBoxImage
        {
            get { return (MessageBoxImage)GetValue(MsgBoxImageProperty); }
            set { SetValue( MsgBoxImageProperty, value); }
        }

        public static readonly DependencyProperty  MsgBoxImageProperty =
            DependencyProperty.Register("MsgBoxImage", typeof(MessageBoxImage), typeof(MessageWindowDialog),
            new UIPropertyMetadata(MessageBoxImage.Information));

        public MessageBoxButton MsgBoxButton
        {
            get { return (MessageBoxButton)GetValue(MsgBoxButtonProperty); }
            set { SetValue(MsgBoxButtonProperty, value); }
        }

        public static readonly DependencyProperty MsgBoxButtonProperty =
            DependencyProperty.Register("MsgBoxButton", typeof(MessageBoxButton), typeof(MessageWindowDialog),
            new UIPropertyMetadata((o, a) =>
            {
                var dialog = o as MessageWindowDialog;
                dialog.ShowMsgBoxButton();
            }));

        private void MarkButtonAction(RC.Button btn, MsgBoxButtons choice)
        {
            btn.Click += delegate { 
                MsgBoxResult = (MessageBoxResult)Enum.Parse(typeof(MessageBoxResult), choice.ToString());
                this.Close();
            };
        }

        public int ButtonCount
        {
            get { return (int)GetValue(ButtonCountProperty); }
            set { SetValue(ButtonCountProperty, value); }
        }

        public static readonly DependencyProperty ButtonCountProperty =
            DependencyProperty.Register("ButtonCount", typeof(int), typeof(MessageWindowDialog), 
            new UIPropertyMetadata(0));

        private void MarkButtonContent(RC.Button btn, MsgBoxButtons choice)
        {
            switch (choice)
            {
                case MsgBoxButtons.None:
                    break;
                case MsgBoxButtons.OK:
                    btn.SetBinding(RC.Button.LabelProperty, new Binding("OkContent"));
                    break;
                case MsgBoxButtons.Yes:
                    btn.SetBinding(RC.Button.LabelProperty, new Binding("YesContent"));
                    break;
                case MsgBoxButtons.No:
                    btn.SetBinding(RC.Button.LabelProperty, new Binding("NoContent"));
                    break;
                case MsgBoxButtons.Cancel:
                    btn.SetBinding(RC.Button.LabelProperty, new Binding("CancelContent"));
                    break;
            }
        }

        private void MarkButton(MsgBoxButtons choice)
        {
            var btn = CreateButton();
            btn.Tag = choice.ToString();
            MarkButtonContent(btn, choice);
            MarkButtonAction(btn, choice);
        }

        private RC.Button CreateButton()
        {
            var btn = new RC.Button();
            Part_ActionButton.Items.Add(btn);
            return btn;
        }

        private void ShowMsgBoxButton()
        {
            Part_ActionButton.Items.Clear();
            switch (MsgBoxButton)
            {
                case MessageBoxButton.OK:
                    MarkButton(MsgBoxButtons.OK);
                    break;
                case MessageBoxButton.OKCancel:
                    MarkButton(MsgBoxButtons.OK);
                    MarkButton(MsgBoxButtons.Cancel);
                    break;
                case MessageBoxButton.YesNo:
                    MarkButton(MsgBoxButtons.Yes);
                    MarkButton(MsgBoxButtons.No);
                    break;
                case MessageBoxButton.YesNoCancel:
                    MarkButton(MsgBoxButtons.Yes);
                    MarkButton(MsgBoxButtons.No);
                    MarkButton(MsgBoxButtons.Cancel);
                    break;
                //default:
                //    break;
            }
            ButtonCount = Part_ActionButton.Items.Count;
        }

        public MessageBoxResult MsgBoxResult { get; set; }

        private void TitleBarButton_Click(object sender, RoutedEventArgs e)
        {
            MsgBoxResult = MessageBoxResult.None;
            this.Close();
        }

    }

    [Flags]
    public enum MsgBoxButtons
    {
        None=0,
        OK=1,
        Yes=2,
        No=33,
        Cancel=4,
        OKCancel=MsgBoxButtons.OK | MsgBoxButtons.Cancel,
        YesNo=MsgBoxButtons.Yes | MsgBoxButtons.No,
        YesNoCancel=MsgBoxButtons.Yes | MsgBoxButtons.No | MsgBoxButtons.Cancel,
    }

    //public enum MsgBoxImage:MessageBoxImage
    //{
        
    //}

    public class MsgBoxImageConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var msgImage = (MessageBoxImage)value;
            var converter = new ImageSourceConverter();
            if (msgImage == MessageBoxImage.None)
                return null;
            var uri = new Uri(string.Format("pack://application:,,,/AppDemo;component/{0}.png", msgImage.ToString()), UriKind.RelativeOrAbsolute);
            return converter.ConvertFrom(uri) as ImageSource;

        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class IsEnumConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var obj = Enum.Parse(value.GetType(), parameter.ToString());
            if (Enum.GetName(value.GetType(), obj) == value.ToString())
                return true;
            return false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    public class ConditionalConverter :IMultiValueConverter
    {
       
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (values[0] is Boolean)
            {
                var condition = System.Convert.ToBoolean(values[0]);
                if (condition) return values[1];
                return values[2];
            }
            if (values[0] != null) return values[1];
            return true;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
